<?php
require_once 'UserLanguage.php';
class User 
{
	/**
	 * Datenbankverbindung
	 *
	 * @var Kanedo_Db
	 */
	protected $_db = null;
	
	/**
	 * Tabellenname
	 *
	 * @var string
	 */
	protected $_table 		= "user_user";
	/**
	 * Primaerschluessel
	 *
	 * @var string
	 */
	protected $_tablePk 	= "id_user_user";
	
	/**
	 * Konstrukt
	 *
	 * @param Kanedo_Db $db
	 * @param string $table
	 * @param string $pk
	 */
	public function __construct(Kanedo_Db $db, $table = "user_user", $pk = "id_user_user")
	{
		$this->_db = $db;
		if (!$this->_db->isConnected())
		{
			throw new Kanedo_Db_Exception("No connection to DB found!");
		}
		$this->_table = $table;
		$this->_tablePk = $pk;
	}
	
	/**
	 * Gibt einen Benutzer anhand seiner ID zurueck
	 *
	 * @param int $id
	 * @return array
	 */
	public function getUserById($id)
	{
		$id = (int) $id;
		$sql = "SELECT * FROM `".$this->_table."` WHERE `".$this->_tablePk."` = ".$id." LIMIT 1;";
		$result = $this->_db->executeQuery($sql);
		$data = $this->_db->fetchAll($result);
		$lang = new UserLanguage($this->_db);
		$data[0]['lang'] = $lang->getUserLanguageById($id);
		
		return $data[0];
	}
	
	/**
	 * Sucht Benutzer anhand einer Spalte
	 *
	 * @param string $col
	 * @param string $val
	 * @return array | bool
	 */
	public function getUserByColumn($col, $val)
	{
		$col = $this->_db->escape($col);
		$val = $this->_db->escape($val);
		$sql = "SELECT * FROM `".$this->_table."` WHERE `".$col."` = '".$val."' LIMIT 1;";
		$result = $this->_db->executeQuery($sql);
		$data = $this->_db->fetchAll($result);
		$lang = new UserLanguage($this->_db);
		$data[0]['lang'] = $lang->getUserLanguageById($data[0]['id_user_user']);
		if ($data)
		return $data[0];
		else return false;
	}
	
	/**
	 * Sucht alle Benutzer und gibt sie als Array zurueck
	 *
	 * @return array
	 */
	public function fetchAllUsers()
	{
		$sql = "SELECT * FROM `".$this->_table."`;";
		$result = $this->_db->executeQuery($sql);
		$data = $this->_db->fetchAll($result);
		return $data;
	}
	/**
	 * Methode zum Hinzufuegen eines Benutzers und gibt seine ID zurueck
	 *
	 * @param string $nick
	 * @param string $name
	 * @param string $prename
	 * @param string $desc
	 * @return int
	 */
	public function insertUser($nick, $password, $name, $prename, $desc)
	{
		$nick = $this->_db->escape($nick);
		$password = md5($password);
		$name = $this->_db->escape($name);
		$prename = $this->_db->escape($prename);
		$desc = $this->_db->escape($desc);
		$hash = md5($nick.$password);
		
		$sql = "INSERT INTO `".$this->_table."` (`nick`,`password`, `name`, `prename`, `desc`, `hash` ) VALUES ('".$nick."','".$password."' '".$name."', '".$prename."', '".$desc."', '".$hash."');";
		$result = $this->_db->executeQuery($sql);
		return mysql_insert_id();
	}
	
	/**
	 * Methode zum Entfernen eines Benutzers
	 *
	 * @param int $id
	 */
	public function deleteUser($id)
	{
		$id = (int) $id;
		$sql = "DELETE FROM `".$this->_table."` WHERE `".$this->_table."`.`".$this->_tablePk."` = ".$id;
		$this->_db->executeQuery($sql);
	}
	
	/**
	 * Aenderung von Benutzerdaten
	 *
	 * @param int $id
	 * @param string $nick
	 * @param string $name
	 * @param string $prename
	 * @param string $desc
	 */
	public function updateUser($id, $nick, $password, $name, $prename, $desc )
	{
		$id = (int) $id;
		$nick = $this->_db->escape($nick);
		$password = md5($password);
		$name = $this->_db->escape($name);
		$prename = $this->_db->escape($prename);
		$desc = $this->_db->escape($desc);
		$hash = md5($nick.$password);
		$sql = "UPDATE `".$this->_table."` SET  `nick` = '".$nick."',
												`password` = '".$password."',
												`name` = '".$name."',
												`prename` = '".$prename."',
												`desc` = '".$desc."', 
												`hash` = '".$hash."'
				WHERE `".$this->_table."`.`".$this->_tablePk."` = ".$id.";";
		$this->_db->executeQuery($sql);
	}
}
?>